package com.szj.pet.hospital.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.szj.pet.hospital.common.result.Result;
import com.szj.pet.hospital.model.hosp.Reserve;
import com.szj.pet.hospital.service.ReserveService;
import com.szj.pet.hospital.vo.hosp.ReserveQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Api(tags = "预约设置管理")
@RestController
@RequestMapping("admin/hosp/reserve")
//@CrossOrigin
public class ReserveController {
    @Autowired
    private ReserveService reserveService;

    //1 查询所有预约信息
    @ApiOperation(value = "获取所有预约信息")
    @GetMapping("findAll")
    public Result findAllReserve() {
        //调用service的方法
        List<Reserve> list = reserveService.list();
        return Result.ok(list);
    }

    //2 逻辑删除预约信息
    @ApiOperation(value = "逻辑删除预约信息")
    @DeleteMapping("{id}")
    public Result removeReserve(@PathVariable Long id) {
        boolean flag = reserveService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //3 条件查询带分页
    @ApiOperation(value = "预约信息条件查询带分页")
    @PostMapping("findPageReserve/{current}/{limit}")
    public Result findPageReserve(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) ReserveQueryVo reserveQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<Reserve> page = new Page<>(current, limit);
        //构建条件
        QueryWrapper<Reserve> wrapper = new QueryWrapper<>();
        String name = reserveQueryVo.getName();//主人名字
        String doctorName = reserveQueryVo.getDoctorName();//医生名字
        String reserveItem = reserveQueryVo.getReserveItem();//预约项目
        String status = reserveQueryVo.getStatus();//状态
        String bufferTime = reserveQueryVo.getBufferTime();//预约时间
        String userId = reserveQueryVo.getUserId();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", reserveQueryVo.getName());
        }
        if (!StringUtils.isEmpty(doctorName)) {
            wrapper.eq("doctor_name", reserveQueryVo.getDoctorName());
        }
        if (!StringUtils.isEmpty(reserveItem)) {
            wrapper.like("reserve_item", reserveQueryVo.getReserveItem());
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", reserveQueryVo.getStatus());
        }
        if (!StringUtils.isEmpty(bufferTime)) {
            wrapper.like("buffer_time", reserveQueryVo.getBufferTime());
        }
        if (!StringUtils.isEmpty(userId)) {
            wrapper.like("user_id", reserveQueryVo.getUserId());
        }
        wrapper.orderByDesc("update_time");
        //调用方法实现分页查询
        IPage<Reserve> pageReserve = reserveService.page(page, wrapper);

        //返回结果
        return Result.ok(pageReserve);
    }

    //4 添加预约信息
    @PostMapping("saveReserve")
    public Result saveReserve(@RequestBody Reserve reserve) throws ParseException {
        //设置状态 1 使用 0 不能使用
        reserve.setStatus(1);
        if(reserve.getReserveItem().equals("健康体检")){
            reserve.setItemNumber(1);
        }else if(reserve.getReserveItem().equals("犬猫驱虫")){
            reserve.setItemNumber(2);
        }else if(reserve.getReserveItem().equals("宠物绝育")){
            reserve.setItemNumber(3);
        }else if(reserve.getReserveItem().equals("洗浴美容")){
            reserve.setItemNumber(4);
        }else {
            reserve.setItemNumber(5);
        }
        System.out.println(reserve.getBufferTime());
        if(!StringUtils.isEmpty(reserve.getBufferTime())){
            System.out.println(reserve.getBufferTime());
            SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
            Date date = ft.parse(reserve.getBufferTime());
            reserve.setReserveTime(date);
            System.out.println(date);
        }
        System.out.println(reserve.getUserId());
        //调用service
        boolean save = reserveService.save(reserve);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //5 根据id获取预约信息
    @GetMapping("getReserve/{id}")
    public Result getReserve(@PathVariable Long id) {

        Reserve reserve = reserveService.getById(id);
        return Result.ok(reserve);
    }
    //6 修改预约信息
    @PostMapping("updateReserve")
    public Result updateReserve(@RequestBody Reserve reserve) {
        boolean flag = reserveService.updateById(reserve);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //7 批量删除预约信息
    @DeleteMapping("batchRemove")
    public Result batchRemoveReserve(@RequestBody List<Long> idList) {
        reserveService.removeByIds(idList);
        return Result.ok();
    }

    //8 预约信息处理和未处理
    @PutMapping("lockReserve/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        //根据id查询医院设置信息
        Reserve reserve = reserveService.getById(id);
        //设置状态
        reserve.setStatus(status);
        //调用方法
        reserveService.updateById(reserve);
        return Result.ok();
    }

    //导出数据字典接口
    @GetMapping("exportData")
    public void exportDict(HttpServletResponse response) {
        reserveService.exportReserveData(response);
    }

    //条件查询带分页
    @ApiOperation(value = "用户病例信息条件查询带分页")
    @PostMapping("findPageReserveDiagnosis/{current}/{limit}")
    public Result findPageReserveDiagnosis(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) ReserveQueryVo reserveQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<Reserve> page = new Page<>(current, limit);
        //构建条件
        QueryWrapper<Reserve> wrapper = new QueryWrapper<>();
        String name = reserveQueryVo.getName();//主人名字
        String reserveItem = reserveQueryVo.getReserveItem();//预约项目
        String   status = reserveQueryVo.getStatus();//状态
        String bufferTime = reserveQueryVo.getBufferTime();//预约时间
        String userId = reserveQueryVo.getUserId();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", reserveQueryVo.getName());
        }
        if (!StringUtils.isEmpty(reserveItem)) {
            wrapper.like("reserve_item", reserveQueryVo.getReserveItem());
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", reserveQueryVo.getStatus());
        }
        if (!StringUtils.isEmpty(bufferTime)) {
            wrapper.like("buffer_time", reserveQueryVo.getBufferTime());
        }
        if (!StringUtils.isEmpty(userId)) {
            wrapper.like("user_id", reserveQueryVo.getUserId());
        }
        wrapper.orderByDesc("update_time");
        wrapper.isNotNull("diagnosis");//diagnosis字段不为空
        wrapper.ne("diagnosis","");//diagnosis字段不为""
        //调用方法实现分页查询
        IPage<Reserve> pageReserve = reserveService.page(page, wrapper);

        //返回结果
        return Result.ok(pageReserve);
    }

    //条件查询带分页
    @ApiOperation(value = "管理员病例信息条件查询带分页")
    @PostMapping("findPageReserveDiagnosisAdmin/{current}/{limit}")
    public Result findPageReserveDiagnosisAdmin(@PathVariable long current,
                                           @PathVariable long limit,
                                           @RequestBody(required = false) ReserveQueryVo reserveQueryVo) {
        //创建page对象，传递当前页，每页记录数
        Page<Reserve> page = new Page<>(current, limit);
        //构建条件
        QueryWrapper<Reserve> wrapper = new QueryWrapper<>();
        String name = reserveQueryVo.getName();//主人名字
        String doctorName = reserveQueryVo.getDoctorName();//医生
        String reserveItem = reserveQueryVo.getReserveItem();//预约项目
        String   status = reserveQueryVo.getStatus();//状态
        String bufferTime = reserveQueryVo.getBufferTime();//预约时间
        String userId = reserveQueryVo.getUserId();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", reserveQueryVo.getName());
        }
        if (!StringUtils.isEmpty(doctorName)) {
            wrapper.like("doctor_name", reserveQueryVo.getDoctorName());
        }
        if (!StringUtils.isEmpty(reserveItem)) {
            wrapper.like("reserve_item", reserveQueryVo.getReserveItem());
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", reserveQueryVo.getStatus());
        }
        if (!StringUtils.isEmpty(bufferTime)) {
            wrapper.like("buffer_time", reserveQueryVo.getBufferTime());
        }
        if (!StringUtils.isEmpty(userId)) {
            wrapper.like("user_id", reserveQueryVo.getUserId());
        }
        wrapper.orderByDesc("update_time");
        wrapper.eq("item_number",5);
        //调用方法实现分页查询
        IPage<Reserve> pageReserve = reserveService.page(page, wrapper);

        //返回结果
        return Result.ok(pageReserve);
    }

}
